package com.bruce.algorithm.leetcode;

import java.util.HashMap;
import java.util.Map;

/**
 * @author luotuan
 * @Description 3 无重复字符的最长字串
 * @create 2020-07-10 17:21
 **/
public class LongestSubstring {
    public static int lengthOfLongestSubstring(String s) {
        int maxLength = 0;
        char[] chars = s.toCharArray();
        Map<String,Integer> strMap = new HashMap<>();
        int post = 0;
        for(int i=0; i<chars.length;i++){
            String target = chars[i]+"";
            if(strMap.containsKey(target)){
                post = (strMap.get(target)>=post)?strMap.get(target)+1:post;
                strMap.put(target,i);
            }else{
                strMap.put(target,i);
            }
            maxLength = Math.max(maxLength,i-post+1);
        }
        return maxLength;
    }
    public static void main(String[] args) {
        String s = "abcdaefe";
        System.out.println(lengthOfLongestSubstring(s));
    }
}

